<template>
<!-- scroll 只有滚动盒子才能触发 -->
  <div class="pageIndex" @scroll="scroll" ref="scrolldom">
    <header v-show="showTitle">
       <img src="../../assets/img/yifei.jpeg" @click="goTop" alt="">
    </header>
    <div class="banner-ct">
      <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
        <van-swipe-item v-for="(item, index) in banner" :key="index">
          <img :src="item.picUrl" alt="" />
        </van-swipe-item>
      </van-swipe>
      <ul>
        <li>
          <img src="../../assets/img/love.png" alt="" />
          <p>签到</p>
        </li>
        <li>
          <img src="../../assets/img/lv.png" alt="" />
          <p>礼券</p>
        </li>
        <li>
          <img src="../../assets/img/wg.png" alt="" />
          <p>砍价</p>
        </li>
        <li>
          <img src="../../assets/img/dp.png" alt="" />
          <p>专栏</p>
        </li>
      </ul>
    </div>
    <my-title>
      <!-- <template #default>
        全民砍价
      </template> -->
      全民砍价
    </my-title>
    <my-title> 精选专题 </my-title>
    <div class="news">
      <ul>
        <li v-for="(item,index) in newsList" :key="index">
          <img :src="item.pic" alt="">
          <h5>{{item.title}}</h5>
          <p>{{item.descript}}</p>
        </li>
      </ul>
    </div>
    <my-title> 人气推荐 </my-title>
    <div class="good-ct">
      <my-cart v-for="(item,index) in goods" :itemObj="item"></my-cart>
    </div>
  </div>
</template>
<script>
import { useRouter } from "vue-router";
import { getBanner, getNews,getGoods } from "../../api/home/index.js";
import { reactive, ref, toRefs } from "vue";
import MyTitle from "../../components/myTitle";
import MyCart from '../../components/myCact';
import { Calendar } from 'vant';
export default {
  components: { MyTitle,MyCart },
  setup() {
    // setup 没有了this指向
    const data = reactive({
      banner: [], // 轮播图的数据
      newsList: [], // 文章列表
      goods: [], // 商品列表
      showTitle: false
    });
    getBanner().then((res) => {
      // 获取轮播图的接口
      data.banner = res.data;
    });
    getNews().then((res) => { // 获取文章列表的
      data.newsList = res.data;
    });

    getGoods({page:3,pageSize:4}).then(res => { // 获取商品列表的数据
      data.goods = res.data;
    })

    // let dom = null,top=0;
    const scrolldom = ref(null);
    let top=0;
    const scroll = (e) => { // 滚动事件 e 事件对象
      // e.target.scrollTop 滚动条距离顶部的高度
      //  console.log(e.target.scrollTop);
      // console.log(e.target);
      // dom = e.target;
      top = e.target.scrollTop;
      if (top>400){
        data.showTitle = true;
      } else {
        data.showTitle = false;
      }
    }
    const goTop = () => { // 回到顶部的函数
      // console.log(e.target);
      // e.target.scrollTop = 0;
      // dom.scrollTop = 0;
      let time = setInterval(() => {
        if (top>0){
          top = top - 10;
          scrolldom.value.scrollTop = top;
        } else {
          clearInterval(time);
        }
      }, 0);
    }
    return {
      ...toRefs(data),
      scroll,
      goTop,
      scrolldom
    };
  },
};
</script>
<style lang="scss" scoped>
.pageIndex {
  overflow: auto;
  .banner-ct {
    position: relative;
    ul {
      position: absolute;
      left: 0;
      bottom: 0;
      display: flex;
      width: 100%;
      background: #fff;
      padding: 20px 0;
      box-sizing: border-box;
      border-radius: 20% 20% 0 0;
      height: 106px;
      li {
        width: 25%;
        display: inline-flex;
        justify-content: center;
        flex-wrap: wrap;
        align-content: space-between;
        img {
          width: 42px;
        }
        p {
          font-size: 15px;
          width: 100%;
          text-align: center;
        }
      }
    }
  }
  .my-swipe {
    img {
      width: 100%;
    }
  }
  .news{
    width: 100%;
    overflow-x: auto;
    ul {
      display: flex;
      background: #fff;
      width: calc(329 * 5px);
      li{
        margin-left: 10px;
      }
      img{
        width: 319px;
        height: 205px;
        border-radius: 5px;
      }
      h5,p{
        font-size: 14px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        width: 319px;
        margin-top: 10px;
      }
      p{
        font-size: 12px;
         margin-bottom: 10px;
      }
    }
  }
  .good-ct{
    display: flex;
    background: #fff;
    flex-wrap: wrap;
    justify-content: space-between;
    padding: 0 10px;
    box-sizing: border-box;
  }
  header{
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 99;
    background: #fff;
    img{
      height: 70%;
      border-radius: 5px;
    }
  }
}
</style>
